package com.hzcx.app.simplechat.ui.group.adapter;

import android.app.Application;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.ui.friend.bean.FriendCityBean;
import com.hzcx.app.simplechat.ui.group.bean.GroupInfoBean;
import com.hzcx.app.simplechat.ui.group.bean.MembersBean;
import com.hzcx.app.simplechat.util.glide.GlideUtils;

import java.util.ArrayList;
import java.util.List;


public class GroupMuteMemberAdapter extends RecyclerView.Adapter<GroupMuteMemberAdapter.GroupMuteMemberVH> {

    public OnItemClickListener onItemClickListener;

    private List<FriendCityBean> dataList = new ArrayList<>();
    private List<MembersBean> membersBeans = new ArrayList<>();

    public GroupMuteMemberAdapter(@Nullable List<FriendCityBean> dataList, @Nullable List<MembersBean> membersBeans) {
        super();
        this.dataList = dataList;
        this.membersBeans = membersBeans;
    }

    @NonNull
    @Override
    public GroupMuteMemberVH onCreateViewHolder(@NonNull ViewGroup parent, int position) {
        Context context = parent.getContext();
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.rv_item_friend_request_content, parent, false);
        return new GroupMuteMemberVH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GroupMuteMemberVH groupMuteMemberVH, int i) {
        if (membersBeans != null && i < membersBeans.size()) {
            MembersBean membersBean = membersBeans.get(i);
            GlideUtils.loadImg(groupMuteMemberVH.icon.getContext(), membersBean.getMember().getAvatar(), groupMuteMemberVH.icon);
            groupMuteMemberVH.nickName.setText(membersBean.getMember().getNickname());
            if (membersBean.getIs_mute()) {
                groupMuteMemberVH.remark.setText(membersBean.getMute_message1());
                groupMuteMemberVH.btn.setText("解禁");
            } else {
                groupMuteMemberVH.btn.setText("同意");
                groupMuteMemberVH.btn.setEnabled(true);
                Drawable bg = groupMuteMemberVH.context.getResources().getDrawable(R.drawable.btn_main_blue_send_bg);
                groupMuteMemberVH.btn.setBackground(bg);
                if (membersBean.getReason() != null) {
                    groupMuteMemberVH.remark.setText(membersBean.getReason());
                } else {
                    groupMuteMemberVH.remark.setText("");
                }

//                if (membersBean.getState() == 2) {
//                    groupMuteMemberVH.btn.setText("已审核");
//                    groupMuteMemberVH.btn.setEnabled(false);
//                    Drawable bg = groupMuteMemberVH.context.getResources().getDrawable(R.drawable.btn_main_blue_confirm_bg_normal);
//                    groupMuteMemberVH.btn.setBackground(bg);
//                } else {
//
//                }
            }

            groupMuteMemberVH.btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(membersBean);
                    }
                }
            });
        } else {
            FriendCityBean friendCityBean = dataList.get(i);
            GlideUtils.loadImg(groupMuteMemberVH.icon.getContext(), friendCityBean.getAvatar(), groupMuteMemberVH.icon);
            groupMuteMemberVH.nickName.setText(friendCityBean.getNickname());
            groupMuteMemberVH.btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(friendCityBean);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (membersBeans != null) {
            return membersBeans.size();
        } else {
            return dataList.size();
        }
    }

    public interface OnItemClickListener {
        void onItemClick(FriendCityBean bean);
        void onItemClick(MembersBean bean);
    }

    class GroupMuteMemberVH extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView nickName;
        Button btn;
        TextView remark;
        TextView tv_state;

        Context context;

        public GroupMuteMemberVH(@NonNull View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.iv_head);
            nickName = itemView.findViewById(R.id.tv_nick_name);
            remark = itemView.findViewById(R.id.tv_remark);
            btn = itemView.findViewById(R.id.btn_send);
            btn.setVisibility(View.VISIBLE);
            btn.setText("解禁");
            tv_state = itemView.findViewById(R.id.tv_state);
            tv_state.setVisibility(View.GONE);
            context = itemView.getContext();
        }
    }
}

